/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：Canvas.js<br>
 * 日期：2016年9月20日<br>
 * 作者: 王志远<br>
 * 功能：绘图帆布<br>
 *
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window, "Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.Canvas<br>
 * 类型功能：绘图帆布<br>
 */
Rainier.UI.V2.Canvas = draw2d.Canvas.extend({
  NAME: "Rainier.UI.V2.Canvas",

  /**
   * 函数名称：Rainier.UI.V2.Canvas.init<br>
   * 函数功能：构造函数<br>
   * 异常信息：无<br>
   */
  init: function (elementId, width, height) {
    this._super(elementId, width, height);
    var _this = this;
    this.setScrollArea("#" + elementId);
    //把keyDown事件解绑，并重新绑定
    $(document).unbind("keydown");
    this.html.off("keydown");
    this.keydownCallback = function (event) {
      // don't initiate the delete command if the event comes from an INPUT field. In this case the user want delete
      // a character in the input field and not the related shape

      var target = $(event.target);
      if (window.app) {
        var context = app.getContext();
        var activeEditor = context.getActiveEditor();
        if (activeEditor && activeEditor.getRunStatus()) {
          if (event.preventDefault) {
            event.preventDefault();
            event.stopPropagation();
          } else {
            event.returnValue = false;
            event.cancelBubble = true;
          }
          return false;
        }
        var currentFigure = activeEditor.context.getRootFigure();
      }
      if (!target.is("input") && !target.is("textarea")) {
        _this.editPolicy.each(function (i, policy) {
          if (policy instanceof draw2d.policy.canvas.KeyboardPolicy) {
            policy.onKeyDown(_this, event.keyCode, event.shiftKey, event.ctrlKey, event);
          }
        });
      }
    };

    this.html.keydown(this.keydownCallback);
    this.regionDragDropConstraint = new draw2d.policy.figure.RegionEditPolicy(0, 0, this.getWidth(), this.getHeight());
    this.on("select", $.proxy(this.onSelectionChanged, this));
    this.on("unselect", $.proxy(this.onUnselectionChanged, this));
  },

  enableWheel: function () {
    var _this = this;
    // this.html.mousewheel(function(event, delta){
    //     var zoom = _this.getZoom();
    //     if(delta>0){
    //         if(zoom > 0.3){
    //             _this.setZoom(zoom - 0.1,true);
    //         }
    //     }else{
    //         if(zoom < 1.8){
    //             _this.setZoom(zoom + 0.1,true);
    //         }
    //     }
    //     if(event.preventDefault){
    //         event.preventDefault();
    //         event.stopPropagation()
    //     }else{
    //         event.returnValue=false;
    //         event.cancelBubble=true
    //     }
    //     return false;
    // });
  },

  /**
   * 函数名称：Rainier.UI.V2.Canvas.onSelectionChanged<br>
   * 函数功能：当选择图元时触发此函数<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  onSelectionChanged: function (emitter, figure) {
    if (figure.figure) {
      if (figure.figure.onSelectionChanged) {
        figure.figure.onSelectionChanged(emitter, figure);
      }
    }
  },

  /**
   * 函数名称：Rainier.UI.V2.Canvas.onUnselectionChanged<br>
   * 函数功能：当取消选择图元时触发此函数<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  onUnselectionChanged: function (emitter, figure) {
    if (figure.figure) {
      if (figure.figure.onUnselectionChanged) {
        figure.figure.onUnselectionChanged(emitter, figure);
      }
    }
  },

  /**
   * @method
   * Add a figure at the given x/y coordinate. This method fires an event.
   *
   * Example:
   *
   *      canvas.on("figure:add", function(emitter, event){
   *         alert("figure added:");
   *      });
   *
   *      // or more general if you want catch all figure related events
   *      //
   *      canvas.on("figure", function(emitter, event){
   *         // use event.figure.getCanvas()===null to determine if the
   *         // figure part of the canvas
   *
   *         alert("figure added or removed:");
   *      });
   *
   * @param {draw2d.Figure} figure The figure to add.
   * @param {Number/draw2d.geo.Point} [x] The new x coordinate of the figure or the x/y coordinate if it is an draw2d.geo.Point
   * @param {Number} [y] The y position.
   **/
  add: function (figure, x, y) {
    if (figure.getCanvas() === this) {
      return;
    }

    if (figure instanceof draw2d.shape.basic.Line) {
      this.lines.add(figure);
      this.linesToRepaintAfterDragDrop = this.lines;
    } else {
      this.figures.add(figure);
      if (typeof y !== "undefined") {
        figure.setPosition(x, y);
      } else if (typeof x !== "undefined") {
        figure.setPosition(x);
      }
    }
    figure.setCanvas(this);

    // to avoid drag&drop outside of this canvas
    figure.installEditPolicy(this.regionDragDropConstraint);

    // important inital call
    figure.getShapeElement();

    // init a repaint of the figure. This enforce that all properties
    // ( color, dim, stroke,...) will be set and pushed to SVG node.
    figure.repaint();

    // fire the figure:add event before the "move" event and after the figure.repaint() call!
    //   - the move event can only be fired if the figure part of the canvas.
    //     and in this case the notification event should be fired to the listener before
    this.fireEvent("figure:add", { figure: figure, canvas: this });

    // fire the event that the figure is part of the canvas
    figure.fireEvent("added", { figure: figure, canvas: this });

    // ...now we can fire the initial move event
    figure.fireEvent("move", { figure: figure, dx: 0, dy: 0 });

    // this is only required if the used router requires the crossing information
    // of the connections
    if (figure instanceof draw2d.shape.basic.PolyLine) {
      this.calculateConnectionIntersection();
      this.linesToRepaintAfterDragDrop.each(function (i, line) {
        line.svgPathString = null;
        line.repaint();
      });
      this.linesToRepaintAfterDragDrop = new draw2d.util.ArrayList();
    }

    return this;
  },
});
